﻿using FreeSql.DataAnnotations;
using Newtonsoft.Json;
using PropertyChanged;
using sys_Print.DataAsset;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using static FreeSql.Internal.GlobalFilter;

namespace sys_Print.Models
{
    [AddINotifyPropertyChangedInterface]
    [JsonObject(MemberSerialization.OptIn)]
    public partial class PrintRecordList
    {
        [JsonProperty, Column(Name = "id", IsPrimary = true, IsIdentity =true)]
        public int Id { get; set; }

        [JsonProperty, Column(Name = "data_print_canvas_model", DbType = "varchar(MAX)")]
        public string DataPrintCanvasModel { get; set; }

        [JsonProperty, Column(Name = "data_print_back_canvas_model", DbType = "varchar(MAX)")]
        public string DataPrintBackCanvasModel { get; set; }

        [JsonProperty, Column(Name = "data_print_model_type", DbType = "varchar(MAX)")]
        public string DataPrintModelType { get; set; }

        [JsonProperty, Column(Name = "factory_no")]
        public string FactoryNo { get; set; }

        [JsonProperty, Column(Name = "print_date", DbType = "datetime2")]
        public DateTime PrintDate { get; set; }

        [JsonProperty, Column(Name = "sales_no", DbType = "varchar(255)")]
        public string SalesNo { get; set; }

        [JsonProperty, Column(Name = "task_no", DbType = "varchar(255)")]
        public string FBillNo { get; set; }

        [JsonProperty, Column(Name = "name_print_model_type", DbType = "varchar(255)")]
        public string NamePrintModelType { get; set; }

        [Column(IsIgnore = true)]
        public string DisplaySide
        {
            get
            {
                // 根据 DataPrintCanvasModel 的内容判断返回 "正面" 或 "反面"
                if (DataPrintCanvasModel != "[]")
                {
                    return "正面" ;
                }

                if (DataPrintBackCanvasModel != "[]")
                {
                    return "反面";
                }

                return "未知"; // 默认值
            }
        }

        public void SetSqlData(PrintModel printModel, ObservableCollection<PrintCanvasModel> printCanvasModels, ObservableCollection<PrintCanvasModel> printBackCanvasModels)
        {
            this.FBillNo = printModel.TaskInfo.FBillNo;
            this.SalesNo = printModel.TaskInfo.SalesNo;
            this.FactoryNo = printModel.SelectedListViewItem.FactoryNo;
            this.NamePrintModelType = printModel.SelectedPrintType.Name;

            this.DataPrintModelType = JsonConvert.SerializeObject(printModel.SelectedPrintType);
            this.DataPrintCanvasModel = JsonConvert.SerializeObject(printCanvasModels);
            this.DataPrintBackCanvasModel = JsonConvert.SerializeObject(printBackCanvasModels);

            this.PrintDate = DateTime.Now;

            App.FsqlDB.Insert(this).ExecuteIdentity();
        }


    }
}
